package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    //获取用户列表
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, String name, String contact, String address) {
        PageHelper.startPage(pageNum,pageSize);
        List<Supplier> supplierList = null;
        if (!StringUtils.isEmpty(name) || !StringUtils.isEmpty(contact) || !StringUtils.isEmpty(address)){
            Example example = new Example(Supplier.class);
            Example.Criteria criteria = example.createCriteria();
            if (!StringUtils.isEmpty(name)){
                criteria.andLike("name","%" + name + "%");
            }
            if (!StringUtils.isEmpty(contact)){
                criteria.andLike("contact","%" + contact + "%");
            }
            if (!StringUtils.isEmpty(address)){
                criteria.andLike("address","%" + address + "%");
            }
            supplierList = supplierMapper.selectByExample(example);
        } else {
            supplierList = supplierMapper.selectAll();
        }
        List<SupplierVO> supplierVOList = supplierList.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).sorted(Comparator.comparingInt(SupplierVO::getSort)).collect(Collectors.toList());
        PageInfo<Supplier> pageInfo = new PageInfo<>(supplierList);
        return new PageVO<>(pageInfo.getTotal(),supplierVOList);
    }

    //添加物资来源
    @Override
    public void addSupplier(Supplier supplier) {
        Calendar calendar = Calendar.getInstance();
        supplier.setCreateTime(calendar.getTime());
        supplier.setModifiedTime(calendar.getTime());
        supplierMapper.insertSelective(supplier);
    }

    //编辑物资来源
    @Override
    public Supplier editSupplier(Long id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    //更新物资来源
    @Override
    public void updateSupplier(Supplier supplier) {
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

    //删除物资来源
    @Override
    public void deleteSupplier(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    //获取所有用户来源
    @Override
    public List<Supplier> findAll() {
        return supplierMapper.selectAll();
    }
}
